package com.hnevc.wechatviewdemo;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity";

    //声明按键的变量
    LinearLayout mTabWeixin,mTabLianxiren,mTabFaxian,mTbWode;
    Fragment mWeixinFragment,mLianxirenFragment,
            mFaxianFragment,mWodeFragmenr;
    ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //从布局文件中，获取控件
        mTabWeixin = (LinearLayout) findViewById(R.id.id_weixin_tab);
        mTabLianxiren = (LinearLayout) findViewById(R.id.id_lianxiren_tab);
        mTabFaxian = (LinearLayout) findViewById(R.id.id_faxian_tab);
        mTbWode = (LinearLayout) findViewById(R.id.id_wode_tab);
        viewPager = (ViewPager) findViewById(R.id.viewpager);

        //讲mTabWeixin设置为选中状态
        mTabWeixin.setSelected(true);
        //mTabLianxiren.setSelected(true);

        //新建Fragment对象
        mWeixinFragment = new WeixinFragment();
        mLianxirenFragment = new LianxirenFragment();
        mFaxianFragment = new FaxianFragment();
        mWodeFragmenr = new WodeFragment();
        //将Fragment添加到List中
        final List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(mWeixinFragment);
        fragmentList.add(mLianxirenFragment);
        fragmentList.add(mFaxianFragment);
        fragmentList.add(mWodeFragmenr);

        //创建适配器
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                //返回position位置需要显示的Fragment
                return fragmentList.get(position);
            }
            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        //设置viepager的适配器对象
        viewPager.setAdapter(adapter);
       viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

           @Override
           public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
               Log.e(TAG,"onPageSelected:"+position);
               if(position==0){
                   mTabWeixin.setSelected(true);
                   mTabLianxiren.setSelected(false);
                   mTabFaxian.setSelected(false);
                   mTbWode.setSelected(false);
               }
               if(position==1){
                   mTabWeixin.setSelected(false);
                   mTabLianxiren.setSelected(true);
                   mTabFaxian.setSelected(false);
                   mTbWode.setSelected(false);
               }
               if(position==2){
                   mTabWeixin.setSelected(false);
                   mTabLianxiren.setSelected(false);
                   mTabFaxian.setSelected(true);
                   mTbWode.setSelected(false);
               }
               if(position==3){
                   mTabWeixin.setSelected(false);
                   mTabLianxiren.setSelected(false);
                   mTabFaxian.setSelected(false);
                   mTbWode.setSelected(true);
               }
           }

           @Override
           public void onPageSelected(int position) {

           }

           @Override
           public void onPageScrollStateChanged(int state) {

           }
       });

    }

    //就是根据点击的按钮，选择不同的Fragment进行显示
    public void tabClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.id_weixin_tab:
                viewPager.setCurrentItem(0);
                mTabWeixin.setSelected(true);
                mTabLianxiren.setSelected(false);
                mTabFaxian.setSelected(false);
                mTbWode.setSelected(false);
                break;
            case  R.id.id_lianxiren_tab:
                viewPager.setCurrentItem(1);
                mTabWeixin.setSelected(false);
                mTabLianxiren.setSelected(true);
                mTabFaxian.setSelected(false);
                mTbWode.setSelected(false);

                break;
            case R.id.id_faxian_tab:
                viewPager.setCurrentItem(2);
                mTabWeixin.setSelected(false);
                mTabLianxiren.setSelected(false);
                mTabFaxian.setSelected(true);
                mTbWode.setSelected(false);
                break;
            case R.id.id_wode_tab:
                viewPager.setCurrentItem(3);
                mTabWeixin.setSelected(false);
                mTabLianxiren.setSelected(false);
                mTabFaxian.setSelected(false);
                mTbWode.setSelected(true);
                break;
        }

    }
}
